// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view;

import java.awt.FlowLayout;
import java.awt.SystemColor;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.gatech.oad.cyberdoc.presenter.Presenter;

public class PatientGroupsPanel extends ApplicationPanel {

	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	static {
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.action."
				+ "ActionHandler");
	}
	private JTable table_3;
	private JList users;
	private JTextField nameField;
	private JTextField typeField;

	/**
	 * Create the panel.
	 */
	public PatientGroupsPanel() {
		setBackground(SystemColor.inactiveCaptionBorder);
		setLayout(null);

		JPanel top = new JPanel();
		top.setBounds(0, 0, 584, 39);
		top.setBackground(SystemColor.inactiveCaptionBorder);
		add(top);
		top.setLayout(null);

		JPanel instructions = new JPanel();
		instructions.setBackground(SystemColor.inactiveCaptionBorder);
		FlowLayout fl_instructions = (FlowLayout) instructions.getLayout();
		fl_instructions.setAlignment(FlowLayout.LEFT);
		instructions.setBounds(10, 11, 306, 24);
		top.add(instructions);

		JLabel lblPleaseSelectA = new JLabel(
				"Please select what type of information you would like to view.");
		instructions.add(lblPleaseSelectA);

		JComboBox comboBox = new JComboBox();
		comboBox.setBounds(326, 11, 248, 20);
		top.add(comboBox);

		JPanel patientInfoCards = new JPanel();
		patientInfoCards.setBounds(10, 38, 281, 165);
		add(patientInfoCards);
		patientInfoCards.setLayout(null);

		JPanel TreatmentRecords = new JPanel();
		TreatmentRecords.setBounds(0, 0, 275, 165);
		patientInfoCards.add(TreatmentRecords);
		TreatmentRecords.setLayout(null);

		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(303, 51, 40, 14);
		add(lblName);

		table_3 = new JTable();
		table_3.setBounds(0, 0, 275, 165);
		TreatmentRecords.add(table_3);

		users = new JList();
		users.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				int index = users.getSelectedIndex();
				
				if (index != -1) {
					String username = users.getModel().getElementAt(index)
							.toString();
					Presenter.getInstance().readUser(username);
				}
			}
		});
		users.setBounds(10, 226, 281, 155);
		add(users);

		JLabel lblPleaseSelectWhat = new JLabel(
				"Please select what patient you would like view.");
		lblPleaseSelectWhat.setBounds(10, 213, 307, 14);
		add(lblPleaseSelectWhat);

		JLabel lblType = new JLabel("Type:");
		lblType.setBounds(303, 78, 40, 14);
		add(lblType);

		nameField = new JTextField();
		nameField.setBounds(355, 38, 134, 28);
		add(nameField);
		nameField.setColumns(10);

		typeField = new JTextField();
		typeField.setEditable(false);
		typeField.setBounds(355, 71, 134, 28);
		add(typeField);
		typeField.setColumns(10);

	}

	@Override
	public void show(JSONObject response) {
		try {
			String action = response.getString("action");

			if (action.equals("view_users")) {
				LOG.info("Showing list of users");

				JSONArray array = response.getJSONArray("users");
				Object[] data = new Object[array.length()];

				for (int i = 0; i < array.length(); i++) {
					JSONObject element = new JSONObject(array.get(i).toString());
					data[i] = element.getString("username");
				}

				LOG.info("Showing " + data.length + " users: "
						+ Arrays.toString(data));

				users.setListData(data);
			} else if (action.equals("read_user")) {
				LOG.info("Showing individual user");

				JSONObject user = response.getJSONObject("user");
				nameField.setText(user.getString("username"));
				typeField.setText(user.getString("type"));
			} else {
				LOG.warning("Panel shown for unexpected action");
			}
		} catch (JSONException e) {
			LOG.log(Level.SEVERE, "Expected key does not exist in response", e);
		}
	}
}
